/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

/**
 *
 * @author Ricardo Orellana
 */

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Restauracion;
import muhnes.sizoo.HibernateUtilZoologia;

public class RestauracionDaoImpl implements RestauracionDao{
 
    Session sesion;
    
    public RestauracionDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Restauracion restauracion ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( restauracion );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Restauracion > restauraciones ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( restauraciones );
        transaccion.commit();
    }

    @Override
    public void actualizar( Restauracion restauracion ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( restauracion );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Restauracion > restauraciones ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( restauraciones );
        transaccion.commit();
    }

    @Override
    public void eliminar( Restauracion restauracion ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( restauracion );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Restauracion > restauraciones ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( restauraciones );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Restauracion restauracion = buscarPorId( id );
        sesion.delete( restauracion );
        transaccion.commit();
    }

    @Override
    public Restauracion buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Restauracion where CodigoRestauracion = " + id);
        Restauracion restauracion = (Restauracion)consulta.uniqueResult();
        transaccion.commit();
        return restauracion;
    }

    @Override
    public List< Restauracion > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Restauracion");
        List< Restauracion > restauraciones = consulta.list();
        transaccion.commit();
        return restauraciones;
    }
    
}
